/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.ASColor;
import org.aswing.Component;
import org.aswing.JTextField;
import org.aswing.UIManager;

import fever.data.validators.StringValidator;
import fever.data.validators.Validator;
import fever.events.ValidationResultEvent;
import fever.utils.Stringifier;
import fever.utils.StringUtil;

import fvaswing.components.input.FvInputComponent;

/**
 * Use a <strong>JTextField</strong> as input component.
 * 
 * @see fvaswing.components.FvInputComponentDialog
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.input.FvTextFieldInput implements FvInputComponent
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _input : JTextField;
	private var _validator : Validator;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Invalid textfield background. */
	public static var invalidColor : ASColor = new ASColor( 0xD70000 );
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	
	/**
	 * Creates and returns a {@link FvTextFieldInput} with passed-in {@code textfield} 
	 * component.
	 */
	public static function create( textfield : JTextField ) : FvTextFieldInput
	{
		var o : FvTextFieldInput = new FvTextFieldInput();
		o._input = textfield;
		o._input.addActionListener( o._checkResult, o );
		
		return o;
	}
	
	/**
	 * Constructor.
	 */
	public function FvTextFieldInput( )
	{
		_validator = new StringValidator();
		_validator.required = true;
		StringValidator( _validator ).minLength = 1;
		
		_input = new JTextField();		_input.addActionListener( _checkResult, this );
	}
	
	/**
	 * Sets validation process for texfied content.
	 */
	public function setValidator( validator : Validator ) : Void
	{
		_validator = validator;	
	}
	
	/**
	 * Returns string validator.
	 */
	public function getValidator(  ) : Validator
	{
		return _validator;
	}
	
	/**
	 * Sets characters restriction for textfield.
	 */
	public function setRestrict( c : String ) : Void
	{
		_input.setRestrict( c );
	}
	
	/**
	 * Returns characters restriction.
	 */
	public function getRestrict() : String
	{
		return _input.getRestrict();	
	}
	
	/** 
	 * Returns a JTextField component to add onto a 
	 * {@link fvaswing.components.FvInputComponentDialog} dialog. 
	 */
	public function getContainer() : Component
	{
		return _input;
	}
	
	/**
	 * Sets text for textfield.
	 */
	public function setValue( str : String ) : Void
	{
		_input.setText( str );
	}
	
	/**
	 * Returns textfield content.
	 */
	public function getValue()
	{
		return _input.getText();
	}
	
	/**
	 * Returns {@code true} if input is valid.
	 */
	public function isValid() : Boolean
	{
		return _checkResult();
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _checkResult() : Boolean
	{
		var s : String = StringUtil.trim( _input.getText() );
		_input.setText( s );
		
		var result : ValidationResultEvent = _validator.validate( s );
		if( result.getType() == ValidationResultEvent.VALID )
		{
			_input.setBackground( UIManager.getColor( 'TextField.background' ) );
			return true;
		}
		else
		{
			_input.setBackground( invalidColor );
			return false;	
		}
	}	
}